<?php

namespace App\Service\CommodityService;
use App\Service\AppService\SystemCacheConfigService;
use App\Service\BaseService;
use App\Service\Code\Normal;
use App\Model\CommodityModel\SupplierCompany;
use Hyperf\DbConnection\Db;

/**
 * 你他云供应商商品资料拉取
 * @author chenshubo
 */
class SupplierCommodityNitayunPullLogService extends BaseService
{
    const PULL_FLAG = 'shopguiyang';
    const TIME_WAIT = 120;//分钟

    /**
     * 开启计划任务
     */
    public function runTask($pullFlag = '')
    {
        $config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('channel_task_user_limit');
        if( empty($config) ) {
            return Normal::returnCode(Normal::FAIL,'参数未配置');
        }
        //所有供应商拉取完成间隔时间
        $SupplierPullWaitTime = self::TIME_WAIT;
        //预计下次拉取商品时间
        $CommodityGetNetPeriod = date("Y-m-d H:i:s",time() + $SupplierPullWaitTime * 60 );
        
        $supplierCode = $pullFlag ?: self::PULL_FLAG;
        $res = SupplierCommodityPullLogService::instance()->getWaitPullSupplierWareHouseInfoBySupplierCode($supplierCode);
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        
        $supplier = $res['data'];
        
        //获取供应商参数
        $company_code   = $supplier['ThirdPartyPlatformCode'] ?: $supplier['SupplierCode'];
        $SupplierID     = $supplier['SupplierID'];
        //初始化拉取状态
        $updateArr = [
            'CommodityGetTaskStatus'    => 0,
            'CommodityGetFinishCount'   => 0,
            'CommodityGetFinishUseTime' => 0.000,
        ];
        SupplierCompany::instance()->where('ID',$SupplierID)->update($updateArr);
        
        $CommodityGetStartTime = microtime(true);
        
        //获取行云的所有分类
        $apiBody = $supplier['JsonBody'];
        $routerClass = '\\App\\Service\\OrderService\\OrderRouteService\\' . ucfirst($supplierCode);
        $resQuery = $routerClass::instance()->queryGoodsList(['apiBody' => $apiBody,'queryBody' => [] ]);
        if( $resQuery['code'] != Normal::SUC ) {
            SupplierCompany::instance()->where('ID',$SupplierID)->update(['CommodityGetContents'=>$resQuery['msg'],'CommodityGetTaskStatus'=>3]);
            return $resQuery;
        }
        
        SupplierCompany::instance()->where('ID',$SupplierID)->update(['CommodityGetContents'=>$resQuery['msg']]);
        
        //动态更新已拉取的数据
        $pull_count = count($resQuery['data']['list']);
        $updateArr = [
            'CommodityGetTaskStatus'    => 1,
            'CommodityGetFinishCount'   => $pull_count
        ];
        SupplierCompany::instance()->where('ID',$SupplierID)->update($updateArr);
        
        $MessageBody  = [
            'SupplierID'        => $SupplierID,
            'SupplierCode'      => $company_code,
            'IsFirstPage'       => 1,
            'DataPage'          => 1,
            'IsLastPage'        => 0,
            'GoodsItemList'     => $resQuery['data']['list']
        ];
        SupplierCommodityPullLogService::instance()->publish($MessageBody);
        
        //更新总数量
        $MessageBody  = [
            'SupplierID'        => $SupplierID,
            'SupplierCode'      => $company_code,
            'IsFirstPage'       => 0,
            'DataPage'          => 1,
            'IsLastPage'        => 1,
            'GoodsItemList'     => []
        ];
        SupplierCommodityPullLogService::instance()->publish($MessageBody);
        
        //拉取完成
        $updateArr = [
            'CommodityGetTaskStatus'    => 2,
            'CommodityGetContents'      => '拉取完成',
            'CommodityGetFinishUseTime' => bcsub(microtime(true),$CommodityGetStartTime,3),
            'CommodityGetNetPeriod'     => $CommodityGetNetPeriod
        ];
        SupplierCompany::instance()->where('ID',$SupplierID)->update($updateArr);
        return Normal::returnCode(Normal::SUC,'拉取完成');
    }
}